package de.dopichaj.labrador.index.meta;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import de.dopichaj.labrador.index.Tag;
import de.dopichaj.labrador.util.io.BitOutputStream;


public class BaselineDocumentMetaDataWriter implements DocumentMetaDataWriter {

    private final BitOutputStream output;
    private final BaselineDocumentMetaDataOutput mdOutput;
    private int id;
    private int position;
    
    private static final class ElementInfo {
        
        public final ElementInfo parent;
        public final Tag tag;
        public int length;
        public final int position;
        public final int id;
        
        public ElementInfo(final ElementInfo parent, final Tag tag, final int id,
            final int position) {
            
            this.parent = parent;
            this.tag = tag;
            this.id = id;
            this.length = 0;
            this.position = position;
        }
        
        public String toString() {
            return id + ": " + tag + ", len=" + length + ", pos=" + position;
        }
    }
    
    private final Stack<ElementInfo> ancestors = new Stack<ElementInfo>();
    private final List<ElementInfo> data = new ArrayList<ElementInfo>();
    
    public BaselineDocumentMetaDataWriter(final BaselineDocumentMetaDataIOFactory ioFactory,
        final BitOutputStream output) {
        this.output = output;
        this.mdOutput = ioFactory.getOutput(output);
        this.id = 0;
    }
    
    
    public void close() throws IOException {
        output.close();
    }

    public void closeElement() throws IOException {
        
        ancestors.pop();
    }

    public void endDocument() throws IOException {
        
        // write the number of elements
        output.writeVariableLengthInt(data.size());
        
        // write the root element (the root has implicit position 0 and no parent)
        final ElementInfo root = data.get(0);
        mdOutput.writeTagID(root.tag.getId());
        mdOutput.writeLengthDelta(root.length);
        
        // write all other elements
        if (data.size() > 1) {
            for (final ElementInfo element : data.subList(1, data.size())) {
    
                final ElementInfo parent = element.parent;
                
                mdOutput.writeIDDelta(element.id - parent.id);
                mdOutput.writeTagID(element.tag.getId());
                mdOutput.writePositionDelta(element.position - parent.position);
                mdOutput.writeLengthDelta(element.length);
            }
        }
        
        data.clear();
        output.flush();
    }

    public int getId() {
        return id;
    }

    public void openElement(final Tag tag) throws IOException {
        final ElementInfo parent = ancestors.isEmpty() ? null : ancestors.peek();
        final ElementInfo elementInfo = new ElementInfo(parent, tag, id++, position);
        data.add(elementInfo);
        ancestors.push(elementInfo);
    }

    public void startDocument(final File file) throws IOException {
        position = 0;
        output.writeString(file.getPath());
        output.writeVariableLengthInt(id);
    }

    public void text(final int wordCount) throws IOException {
        position += wordCount;
        ancestors.peek().length += wordCount;
    }

}
/*
Copyright (c) 2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/